package topcbl.room;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class Player {
	String mName = "New Player";
	Socket mSocket = null;
	BufferedReader input;
	PrintWriter output;
	Team team = null;

	public Player(String name) {
		mName = name;
	}

	public Player(Socket socket) {
		// This for client
		mSocket = socket;
		try {
			input = new BufferedReader(new InputStreamReader(
					mSocket.getInputStream()));
			output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
					mSocket.getOutputStream())), true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Hàm lấy tên người chơi từ client
	 */
	public void createName() {
		try {
			mName = input.readLine();
			return;
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void sendMessage(String data) {
		if (mSocket != null)
			output.println(data);
	}

	public String receiveMessage() throws Exception {
		if (mSocket != null)
			return input.readLine();
		return null;
	}

	public String getName() {
		return mName;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public boolean isServer() {
		if (mSocket == null)
			return true;
		return false;
	}

	public boolean closeConnect() {
		if (mSocket != null) {
			try {
				mSocket.close();
				return true;
			} catch (IOException e) {
				return false;
			}
		} else
			return true;
	}

}
